export default {
	data() {
		return {
			allowLeave: true
		};
	},
	mounted() {
		// window.addEventListener("beforeunload", this.onBeforeunload, false);
	},
	beforeDestroy() {
		// window.removeEventListener("beforeunload", this.onBeforeunload, false);
	},
	methods: {
		// beforeunload监听事件
		onBeforeunload(e) {
			e = e || window.event;
			let msg = "系统可能不会保存您所做的更改。";
			// e.returnValue = '确定要关闭窗口吗？'
			// 兼容IE8和Firefox 4之前的版本
			if (e) {
				e.returnValue = msg;
			}

			// Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
			this.allowLeave = true;
			return msg;
		},
		async routeLeaveConfirm() {
			let confirm = await this.$confirm("系统可能不会保存您所做的更改。", "提示", {}).catch(
				() => false
			);
			return confirm;
		}
	},
	async beforeRouteLeave(to, from, next) {
		if (!this.allowLeave) {
			let confirm = await this.routeLeaveConfirm();
			if (!confirm) return;
		}
		next();
	}
};
